package fragments;

import java.io.IOException;
import java.net.URL;
import java.util.List;

import dataFormat.IcultureActivity;
import com.matchcalendar.R;
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.AsyncTask;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.util.SparseArray;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;

public class NearActivitiesFragment extends Fragment {
	private List<IcultureActivity> acList;
	private IcultureActivity selAc = null;
	
	public NearActivitiesFragment(List<IcultureActivity> acList) {
		this.acList = acList;
	}
	
	public IcultureActivity getSelAc() {
		return selAc;
	}

	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
		ListView nearActivitiesListView = new ListView(getActivity());
		
		nearActivitiesListView.setAdapter(new NearActivitiesAdapter(getActivity(),
				android.R.layout.simple_list_item_1, acList));
		nearActivitiesListView.setOnItemClickListener(new NearActivitiesListViewItemClick());
		return nearActivitiesListView;
	}
	
	private class NearActivitiesAdapter extends ArrayAdapter<IcultureActivity> {
		private SparseArray<View> findRow = new SparseArray<View>();
		
		public NearActivitiesAdapter(Context context, int textViewResourceId,
				List<IcultureActivity> objects) {
			super(context, textViewResourceId, objects);
		}
		
		@Override
		public View getView(int position, View convertView, ViewGroup parent) {
			if (findRow.get(position) == null) {
				View view = View.inflate(getActivity(), R.layout.custom_near_activities_view, null);
				String distanceStr;
				
				if (getItem(position).getDistance() >= 1000)
					distanceStr = Math.round(getItem(position).getDistance() / 100.) / 10. + "km";
				else
					distanceStr = getItem(position).getDistance() + "m";
				((TextView)view.findViewById(R.id.textViewTitle)).setText(getItem(position).getTitle());
				((TextView)view.findViewById(R.id.textViewPlace)).setText("活動地點:\n" +
				getItem(position).getLocationName());
				((TextView)view.findViewById(R.id.textViewTime)).setText("活動時間:\n" +
				getItem(position).getTime().split("~")[0]);
				((TextView)view.findViewById(R.id.textViewDistance)).setText(distanceStr);
				findRow.put(position, view);
				new GetPictures(position).execute();
				return view;
			}
			else
				return findRow.get(position);
		}
		
		private class GetPictures extends AsyncTask<Void, Void, Bitmap> {
			private int position;

			public GetPictures(int position) {
				this.position = position;
			}

			@Override
			protected Bitmap doInBackground(Void... params) {
				Bitmap picture = null;
				
				try {
					URL url = new URL(getItem(position).getImageUrl());
					
					picture = BitmapFactory.decodeStream(url.openConnection().getInputStream());
				} catch (IOException e) {
				}
				return picture;
			}
			
			@Override
			protected void onPostExecute(Bitmap picture) {
				if (picture != null) {
					((ImageView)findRow.get(position).findViewById(R.id.imageViewPicture)).
					setImageBitmap(picture);
					notifyDataSetChanged();
				}
			}
		}
	}
	
	private class NearActivitiesListViewItemClick implements ListView.OnItemClickListener {
		private ImageView previousClickImageView = null;

		@Override
		public void onItemClick(AdapterView<?> arg0, View view, int position, long arg3) {
			ImageView imageViewCheck = (ImageView)view.findViewById(R.id.imageViewCheck);
			
			if (previousClickImageView != null)
				previousClickImageView.setImageDrawable(null);
			previousClickImageView = imageViewCheck;
			imageViewCheck.setImageResource(R.drawable.tick);
			selAc = acList.get(position);
		}
	}
}
